package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Configuration
@RestControllerAdvice   //异常处理拦截
public class ExceptionCatch {
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception ex) {
        ex.printStackTrace();
        //记录日志
        log.error("捕获到全局异常：ex{}" + ex);

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "你网络异常，稍后重试");
    }

    /**
     * 拦截自定义异常
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult custException(CustomException ex) {
        ex.printStackTrace();
        log.error("拦截自定义异常", ex);
        AppHttpCodeEnum codeEnum = ex.getAppHttpCodeEnum();
        return ResponseResult.errorResult(codeEnum);
    }

}
